# encoding: utf-8
class AdminController < ApplicationController
  layout 'admin'
  before_filter :require_admin, :check_access
  helper_method :date_parts,:app_names,:app_display_name
  helper_method :sort_column,:sort_direction
  require "csv"

  def require_admin
    unless current_admin
      redirect_to new_admin_session_path
    end
  end

  def check_access
    action = params
    unless current_admin.can_do?(action,session)
      render :file => "public/403.html", :status => 403, :layout => false
    end
  end

  def user_for_paper_trail
    current_admin.id if current_admin
  end

  private

  def date_parts
    {"day" => '天',"week" => '周',"month" => '月',"year" => '年'}
  end

  def app_names
    AppVersion::VERSION_DISPLAY_NAMES.merge({'all' => '全部'})
  end

  def app_display_name(name)
    if AppVersion::VERSION_DISPLAY_NAMES[name]
       AppVersion::VERSION_DISPLAY_NAMES[name]
    else
      '未知'
    end
  end

  def sort_column
    params[:controller].gsub('admin/','').classify.singularize.constantize.column_names.include?(params[:sort]) ? params[:sort] : "id"
  end
  
  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
  end

end
